package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.context.WebContext;
import com.mindskip.xzs.domain.dto.KeyValue;
import com.mindskip.xzs.domain.dto.admin.user.UserEventLogPageDTO;
import com.mindskip.xzs.domain.dto.admin.user.UserUpdateDTO;
import com.mindskip.xzs.domain.vo.admin.user.UserEventLogPageVO;
import com.mindskip.xzs.service.UserEventLogService;
import com.mindskip.xzs.utility.RestResponse;
import com.mindskip.xzs.domain.dto.admin.user.UserCreateDTO;
import com.mindskip.xzs.domain.dto.admin.user.UserPageDTO;
import com.mindskip.xzs.domain.entity.User;
import com.mindskip.xzs.domain.vo.admin.user.UserVO;
import com.mindskip.xzs.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(tags = "用户管理接口")
@RestController("AdminUserController")
@RequestMapping(value = "/api/admin/user")
public class UserController {

    private final UserService userService;

    private final UserEventLogService userEventLogService;

    private final WebContext webContext;

    public UserController(UserService userService, UserEventLogService userEventLogService, WebContext webContext) {
        this.userService = userService;
        this.userEventLogService = userEventLogService;
        this.webContext = webContext;
    }

    @ApiOperation(value = "用户分页",httpMethod = "POST")
    @PostMapping("/page/list")
    public RestResponse<PageInfo<UserVO>> pageList(@RequestBody UserPageDTO userPageDTO) {

        PageInfo<UserVO> pageInfo = userService.getUserByPage(userPageDTO);

        return RestResponse.ok(pageInfo);
    }

    @ApiOperation(value = "用户查询",httpMethod = "POST")
    @PostMapping("/select/{id}")
    public RestResponse<UserVO> select(@PathVariable Integer id) {
        User user = userService.selectById(id);
        UserVO userVO = UserVO.from(user);

        return RestResponse.ok(userVO);
    }

    @ApiOperation(value = "用户编辑",httpMethod = "POST")
    @PostMapping("/edit")
    public RestResponse<User> edit(@RequestBody @Valid UserCreateDTO userCreateDTO) {

        User user = userService.insertOrUpdateUser(userCreateDTO);
        return RestResponse.ok(user);
    }

    @ApiOperation(value = "用户删除",notes = "软删除", httpMethod = "POST")
    @PostMapping("/delete/{id}")
    public RestResponse<Object> delete(@PathVariable Integer id) {

        // 硬删除
        //userService.deleteById(id);

        // 软删除
        userService.softDeleteById(id);

        return RestResponse.ok();
    }

    @ApiOperation(value = "用户状态修改",httpMethod = "POST")
    @PostMapping("/changeStatus/{id}")
    public RestResponse<Integer> changeStatus(@PathVariable Integer id) {
        Integer integer = userService.changeStatus(id);
        return RestResponse.ok(integer);
    }

    @PostMapping("/event/page/list")
    public RestResponse<PageInfo<UserEventLogPageVO>> page(@RequestBody UserEventLogPageDTO userEventLogPageDTO) {

        PageInfo<UserEventLogPageVO> userEventLogByPage = userEventLogService.getUserEventLogByPage(userEventLogPageDTO);
        return RestResponse.ok(userEventLogByPage);
    }

    @PostMapping("/current")
    public RestResponse<UserVO> current() {

        User currentUser = webContext.getCurrentUser();
        UserVO userVO = UserVO.from(currentUser);

        return RestResponse.ok(userVO);
    }

    @PostMapping("/update")
    public RestResponse<Object> update(@RequestBody @Valid UserUpdateDTO userUpdateDTO) {

        User user = userService.selectById(webContext.getCurrentUser().getId());
        BeanUtils.copyProperties(userUpdateDTO, user);
        user.setModifyTime(new Date());
        userService.updateByIdFilter(user);
        return RestResponse.ok();
    }

    @PostMapping("/selectByUserName")
    public RestResponse<List<KeyValue>> selectByUserName(@RequestBody String userName) {
        List<KeyValue> keyValues = userService.selectByUserName(userName);
        return RestResponse.ok(keyValues);
    }
}
